import request from "../util/request";
import {importExcel, regTest} from "@/util/util";
import _ from 'lodash'

/**
 * 员工信息
 * @type {{lastSchool: string, idCard: string, contactType: string, remark: string, roleType: string, firstMajor: string, post: string, politic: string, departId: string, contactPeriod: string, email: string, contactStart: string, address: string, lastEduExp: string, firstDegree: string, avatar: string, lastDegree: string, firstEduExp: string, phone: string, ethic: string, healthy: string, name: string, nativePlace: string, lastMajor: string, trainExp: string, position: string, firstSchool: string, maritalStatus: string}}
 */
export const newEmployee = {
    name: '',
    idCard: '',
    ethic: '',
    nativePlace: '',
    address: '',
    healthy: '',
    avatar: '',
    maritalStatus: '',
    politic: '',
    email: '',
    phone: '',
    post: '无',
    departId: '',
    position: '无',
    roleType: '',
    contactType: '',
    contactStart: '',
    contactPeriod: '',
    remark: '',
    // 教育经历
    firstEduExp: '',
    firstDegree: '',
    firstMajor: '',
    firstSchool: '',
    lastEduExp: '',
    lastDegree: '',
    lastMajor: '',
    lastSchool: '',
    // 培训经历
    trainExp: ''
}
// 数据导入表头
export const empExcelHeaders = {
    '姓名' : 'name',
    '身份证号' : 'idCard',
    '民族' : 'ethic',
    '籍贯' : 'nativePlace',
    '家庭地址' : 'address',
    '健康状况' : 'healthy',
    '婚姻状态' : 'maritalStatus',
    '政治面貌' : 'politic',
    '电子邮箱' : 'email',
    '手机号' : 'phone',
    '职务': 'post',
    '职位' : 'position',
    '所属部门' : 'departId',
    '人员类型' : 'roleType',
    '合同起始时间' : 'contactStart',
    '合同期限' : 'contactPeriod',
    '合同类型' : 'contactType',
    '第一学历' : 'firstEduExp',
    '第一学位' : 'firstDegree',
    '毕业学校1' : 'firstMajor',
    '所学专业1' : 'firstSchool',
    '最高学历' : 'lastEduExp',
    '最高学位' : 'lastDegree',
    '毕业学校2' : 'lastMajor',
    '所学专业2' : 'lastSchool',
    '培训经历' : 'trainExp',
    '备注' : 'remark'
}

/**
 * 获取数据字典
 * @returns {*}
 */
export const getDics = async function () {
    let dics = {}
    dics.provinces = await request.get("/common/provinces").then(res => res.data),
    dics.politics = await request.get("/common/politics").then(res => res.data),
    dics.ethics = await request.get("/common/ethics").then(res => res.data),
    dics.eduExps = await request.get("/common/edu_exps").then(res => res.data),
    dics.eduDegrees = await request.get("/common/edu_degrees").then(res => res.data),
    dics.contactTypes = await request.get("/common/contact_types").then(res => res.data),
    dics.contactPeriods = await request.get("/common/contact_periods").then(res => res.data),
    dics.roleTypes = await request.get("/common/role_types").then(res => res.data),
    dics.maritalStatus = await request.get("/common/marital_status").then(res => res.data),
    dics.healtyTypes = await request.get("/common/healthy_types").then(res => res.data)
    dics.departs = await request.get(`/depart/items`).then(res => res.data)
    return dics
}

/**
 * 获取员工列表
 * @param current
 * @param size
 */
export const list = function (departId, current, size) {
    return request.get(`/employee/list/${departId}/${current}/${size}`)
}

/**
 * 保存员工信息
 * @param employee
 */
export const save = function (employee) {
    return request.put('/employee/save', employee)
}

/**
 * 删除员工信息
 * @param employeeId
 */
export const deleteEmp = function (employeeId) {
    return request.delete(`/employee/delete/${employeeId}`)
}

/**
 * 上传图片
 * @param formData
 * @returns {Promise<AxiosResponse<any>>}
 */
export const uploadPic = function (formData) {
    return request.post('/employee/upload/photo', formData, {
        headers: { 'Content-Type': 'multipart/form-data' }
    })
}

/**
 * 从Excel读取数据
 * @param file
 * @param dics
 * @returns {Promise<unknown>}
 */
export const importDataFromExcel = function (file, dics) {
    return importExcel(file, empExcelHeaders).then(list => {
        return new Promise(function (resolve, reject) {
            _.forEach(list, function(emp) {
                // 0、姓名
                if(!regTest("name", emp.name)) {
                    reject(emp.name + '-姓名不正确')
                }
                // 1、民族
                if(_.findIndex(dics.ethics, function (e) { return e.name === emp.ethic }) < 0) {
                    reject(emp.name + '-民族信息有误')
                }
                // 2、籍贯
                if(_.findIndex(dics.provinces, function (e) { return e.name === emp.nativePlace }) < 0) {
                    reject(emp.name + '-籍贯信息有误')
                }
                // 3、政治面貌
                if(_.findIndex(dics.politics, function (e) { return e.name === emp.politic }) < 0) {
                    reject(emp.name + '-政治面貌信息有误')
                }
                // 4、部门
                let dept = _.find(dics.departs, function (e) { return e.name === emp.departId })
                if(!dept) {
                    reject(emp.name + '-所属部门不存在')
                }
                emp.departId = dept.id
                // 5、合同类型
                if(_.findIndex(dics.contactTypes, function (e) { return e.name === emp.contactType }) < 0) {
                    reject(emp.name + '-合同类型信息有误')
                }
                // 6、第一学历
                if(!_.isEmpty(emp.firstEduExp) && _.findIndex(dics.eduExps, function (e) { return e.name === emp.firstEduExp }) < 0) {
                    reject(emp.name + '-第一学历信息有误')
                }
                // 7、第一学位
                if(!_.isEmpty(emp.firstDegree) && _.findIndex(dics.eduDegrees, function (e) { return e.name === emp.firstDegree }) < 0) {
                    reject(emp.name + '-第一学位信息有误')
                }
                // 8、最高学历
                if(!_.isEmpty(emp.lastEduExp) && _.findIndex(dics.eduExps, function (e) { return e.name === emp.lastEduExp }) < 0) {
                    reject(emp.name + '-最高学历信息有误')
                }
                // 9、最高学位
                if(!_.isEmpty(emp.lastDegree) && _.findIndex(dics.eduDegrees, function (e) { return e.name === emp.lastDegree }) < 0) {
                    reject(emp.name + '-最高学位信息有误')
                }
                // 10、健康状况
                if(_.findIndex(dics.healtyTypes, function (e) { return e.name === emp.healthy }) < 0) {
                    reject(emp.name + '-健康状况信息有误')
                }
                // 11、婚姻状况
                if(_.findIndex(dics.maritalStatus, function (e) { return e.name === emp.maritalStatus }) < 0) {
                    reject(emp.name + '-婚姻状态信息有误')
                }
                // 12、合同起始时间
                if(!regTest("date", emp.contactStart)) {
                    reject(emp.name + '-合同起始时间格式有误，应该是YYYY-MM-DD')
                }
                // 13、判断电话号码
                if(!regTest("phone", emp.phone)) {
                    reject(emp.name + '-手机号码格式有误')
                }
                // 14、身份证号
                if(!regTest("idCard", emp.idCard)) {
                    reject(emp.name + '-身份证号码有误')
                }
                // 15、家庭地址
                if(_.isEmpty(emp.address)) {
                    reject(emp.name + '-家庭地址为空')
                }
                // 16、人员类型
                if(_.findIndex(dics.roleTypes, function (e) { return e.name === emp.roleType }) < 0) {
                    reject(emp.name + '-人员类型不正确')
                }
            })
            resolve(list)
        })
    })
}

/**
 * 上传员工信息列表
 * @param employees
 */
export const uploadEmployees = function (employees) {
    return request.put('/employee/import', employees)
}